#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : importLaos.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/6/11 14:30                 #
#                                                                   #
#                   @Last Update  : 2021/6/11 14:30                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:老挝无法复制信息LAW文件的处理                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import hashlib
import os
import re
import shutil

package_path = r"E:\orinigal"
package_path1 = r"E:\工作记录\工作内容\“一带一路”项目\各国专家提供的法律资源资料\老挝\导入\Law\regulations\pdf"
for root, dirs, files in os.walk(package_path, topdown=False):
    for file in files:
        if file.__contains__('.pdf'):
            # 缅甸判例获取裁决日期
            # adjustationdate = ''
            # adjustationdatel = re.findall("\d{4}\.\d{1,2}\.\d{1,2}",file)
            # if len(adjustationdatel):
            #     adjustationdate = adjustationdatel[0]
            # 老挝regulastion的发文机构
            Organizaationl = root.split('\\')
            Organizaation = Organizaationl[-1]
            Organizaation = re.sub("^Fw_ [0-9]{1,2}\.\s+", "", Organizaation)
            # 老挝regulastion的法律标题
            Title = re.sub("((\s)?- Copy+)$", "", re.sub("\.pdf$", "", re.sub("[0-9]{1,2}\s+", "", file)))
            # 使用文件名计算sysid值
            sysid = str(hashlib.md5(file.encode('utf-8')).hexdigest())
            SYS_FLD_DIGITFILENAME = 'f' + sysid + '.pdf'
            # 原文件路径
            file_path = os.path.join(root, file)
            # 新文件路径
            new_file_path = os.path.join(package_path1, SYS_FLD_DIGITFILENAME)
            # 复制文件并修改文件名
            shutil.copyfile(file_path, new_file_path)
            # 存入sql中的数据
            with open("detail.txt", 'a', encoding='utf-8') as f:
                f.write("%s;%s;%s;%s;%s\n" % (file, Title, Organizaation, sysid, SYS_FLD_DIGITFILENAME))
        # 打印出非pdf文件
        if not file.__contains__('.pdf'):
            nofile_path = os.path.join(root,file)
            print(nofile_path)


